package com.whmnx.main.ui.model;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.TreeBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.view.CommonDialog;
import com.whmnx.base.view.CommonDialog2;
import com.whmnx.main.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * author : huang
 * date   : 2020/9/28 0028 17:11
 * version: 1.0
 * @author Administrator
 */
public class ComcClassItemViewModel extends ItemViewModel {

    public ObservableField<TreeBean> entity = new ObservableField<>();
    private ComClassModel viewModel;

    public ComcClassItemViewModel(@NonNull ComClassModel viewModel, TreeBean treeBean) {
        super(viewModel);
        this.entity.set(treeBean);
        this.viewModel = viewModel;
    }


    //修改商品分类
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            String s2 = "添加分类";
            new CommonDialog(viewModel.mFragment.getActivity(), R.style.dialog, s2,1, (dialog, confirm,input,s) -> {
                if (confirm) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("category_Name",input);
                    params.put("category_Id",entity.get().getKey());
                    params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
                    HttpClient.post(viewModel.mFragment.getActivity(), Constants.ADDORUPDATE2, params,new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            viewModel.getCategorytree();
                        }
                    });
                }
                dialog.dismiss();
            }).show();
        }
    });


    //删除商品分类
    public BindingCommand delOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            String s2 = "确定删除吗";
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    Map<String, Object> params = new HashMap<>();
                    List<String> list = new ArrayList<>();
                    list.add(entity.get().getKey());
                    params.put("",list);
                    HttpClient.post(viewModel.mFragment.getActivity(), Constants.DELETE, params,new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            viewModel.deleteItem(ComcClassItemViewModel.this);
                        }
                    });
                }
                dialog.dismiss();
            }).show();
        }
    });
}
